﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using UserCtrl;
using System.Collections;

namespace GRWL.BZSC.UI
{
    public partial class SelectBillFrm : BaseFrm
    {
        /*
         * 输入
         * 
         *  AllowMulitSelect
         *  SelectBillType
         *  或
         *  SetCondition
         *  
         * 输出
         * 
         *  SelectBills
         */

        public class SelectBillsInfo
        {
            /// <summary>
            /// 主单序号
            /// </summary>
            public int BillId { get; set; }
            /// <summary>
            /// 主单编号
            /// </summary>
            public string BillCode { get; set; }
            /// <summary>
            /// 细单序号
            /// </summary>
            public int BillDetailId { get; set; }
            /// <summary>
            /// 物料/产品编号
            /// </summary>
            public int MateriailId { get; set; }
        }

        /// <summary>
        /// 表格当前选中集
        /// </summary>
        public object[] SelectedItems { get; set; }

        /// <summary>
        /// 是否允许多选或单选
        /// <summary>
        public bool AllowMulitSelect { get; set; }

        /// <summary>
        /// 选择单据类型
        /// </summary>
        public GRWL.BZSC.Common.EnumCommon.BillType SelectBillType { get; set; }

        private bool bSupplier = true;

        ///// <summary>
        ///// 单据编号字头
        ///// </summary>
        //private string BillPrefix;

        private class TableStruction
        {
            private string _TableFieldName;
            private string _TableFieldCaption;
            private bool _Visible = true;

            public TableStruction(string TableFieldName, string TableFieldCaption)
            {
                _TableFieldName = TableFieldName;
                _TableFieldCaption = TableFieldCaption;
                _Visible = true;
            }

            public TableStruction(string TableFieldName, string TableFieldCaption, bool Visible)
            {
                _TableFieldName = TableFieldName;
                _TableFieldCaption = TableFieldCaption;
                _Visible = Visible;
            }

            public string TableFieldName
            {
                get { return _TableFieldName; }
            }

            public string TableFieldCaption
            {
                get { return _TableFieldCaption; }
            }

            public bool Visible
            {
                get { return _Visible; }
            }

        }

        /// <summary>
        /// 设定搜索条件环境, SearchType 选择单据类型, ThePrefix 单据编号字头, MultiRec 是否允许多选或单选
        /// </summary>
        public void SetCondition(GRWL.BZSC.Common.EnumCommon.BillType SearchType, string ThePrefix, bool MultiRec)
        {
            /// <summary>
            /// 选择单据类型
            /// </summary>
            SelectBillType = SearchType;
            /// <summary>
            /// 单据编号字头
            /// </summary>
            BillPrefix = ThePrefix;
            /// <summary>
            /// 是否允许多选或单选
            /// </summary>
            AllowMulitSelect = MultiRec;
        }

        /// <summary>
        /// 返回结果集合
        /// </summary>
        public List<SelectBillsInfo> SelectBills = new List<SelectBillsInfo>();

        public SelectBillFrm()
        {
            InitializeComponent();

            //默认不允许多选
            AllowMulitSelect = false;

            this.BaseDataProxy = new BaseDataSvc.BaseDataClient();
            this.BaseDataProxy.Open();
        }

        private void SelectBillFrm_Load(object sender, EventArgs e)
        {
            //init
            Text = "选择"; //SelectBillType.ToString() + 
            gvBill.MultiSelect = AllowMulitSelect;
            gvBill.ReadOnly = true;
            gvBill.AutoGenerateColumns = false;
            gvBill.CellDoubleClick += new DataGridViewCellEventHandler(gvBill_CellDoubleClick);
            gvBill.RowsAdded += new DataGridViewRowsAddedEventHandler(gvBill_RowsAdded);

            //默认从昨天开始到今天
            edtBeginDate.Value = edtBeginDate.Value.AddDays(-1);

            switch (SelectBillType)
            {
                case GRWL.BZSC.Common.EnumCommon.BillType.CG:
                case GRWL.BZSC.Common.EnumCommon.BillType.ZS:
                case GRWL.BZSC.Common.EnumCommon.BillType.QG:
                case GRWL.BZSC.Common.EnumCommon.BillType.CGSH:
                case GRWL.BZSC.Common.EnumCommon.BillType.SCSH:
                case GRWL.BZSC.Common.EnumCommon.BillType.BJ:
                    bSupplier = true;
                    label2.Text = "供应商";
                    break;
                case GRWL.BZSC.Common.EnumCommon.BillType.DH:
                    bSupplier = false;
                    label2.Text = "  客户";
                    break;
                default:
                    label2.Visible = false;
                    traderSelector.Visible = false;
                    break;
            }
        }

        void gvBill_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo((UDataGridView)sender);
        }

        private void gvBill_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0)
                return;

            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update))
            {
                return;
            }

            GetResult();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sSupplier;
            if (traderSelector.CurrentSelectItem == null)
                sSupplier = "";
            else
                sSupplier = traderSelector.CurrentSelectItem.Name;
            try
            {
                Cursor = Cursors.WaitCursor;
                SearchBills(edtCode.Text, edtOperator.Text, edtStatus.Text, edtBeginDate.Value.Date, edtEndDate.Value, sSupplier);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void SearchBills(string Code, string Operator, string Status, DateTime BeginDate, DateTime EndDate, string Supplier)
        {
            if (string.IsNullOrEmpty(Code))
                Code = null;
            if (string.IsNullOrEmpty(Operator))
                Operator = null;
            if (string.IsNullOrEmpty(Status))
                Status = null;
            if (string.IsNullOrEmpty(Supplier))
                Supplier = null;

            DataTable tbResult = GetSearchResult(Code, Operator, Status, BeginDate, EndDate, Supplier);
            if (tbResult != null)
                CreateViewFromResult(tbResult);
        }

        private DataTable GetSearchResult(string Code, string Operator, string Status, DateTime BeginDate, DateTime EndDate, string Supplier)
        {
            //
            DataTable tbResult = null;
            switch (SelectBillType)
            {
                case GRWL.BZSC.Common.EnumCommon.BillType.CG:
                    {
                        QOrder[] xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.CG.ToString(), Code, Operator, Status, BeginDate, EndDate, null, null, null, null,null,null);
                        // 按照 日期，单号 排序
                        IEnumerable<QOrder> query =
                            from QOrder in xQOrder
                            orderby QOrder.OrderDate, QOrder.OrderCode
                            select QOrder;
                        tbResult = query.ToDataTable(); // xQOrder.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.DH:
                    {
                        QOrder[] xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.DH.ToString(), Code, Operator, Status, BeginDate, EndDate, null, null, null, null,null,null);
                        // 按照 日期，单号 排序
                        IEnumerable<QOrder> query =
                            from QOrder in xQOrder
                            orderby QOrder.OrderDate, QOrder.OrderCode
                            select QOrder;
                        tbResult = query.ToDataTable(); // xQOrder.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCD:
                    {
                        QProduction[] xQProduction = BaseDataProxy.FindProductions(Code, Operator, Status, BeginDate, EndDate, null, null, null,null);
                        // 按照 日期，单号 排序
                        IEnumerable<QProduction> query =
                            from QProduction in xQProduction
                            orderby QProduction.PrdnDate, QProduction.Code
                            select QProduction;
                        tbResult = query.ToDataTable(); // xQProduction.ToDataTable();                        
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.CGZJ://采购质检
                    {
                        QQC[] qqc = BaseDataProxy.FindQCs(GRWL.BZSC.Common.EnumCommon.BillType.CGZJ.ToString(),Code, Operator, Status, BeginDate, EndDate);
                        tbResult = qqc.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCZJ://生产质检
                    {
                        QQC[] qqc = BaseDataProxy.FindQCs(GRWL.BZSC.Common.EnumCommon.BillType.SCZJ.ToString(), Code, Operator, Status, BeginDate, EndDate);
                        tbResult = qqc.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.ZS:
                    {
                        TB_QCResult[] tbTemp = BaseDataProxy.FindQCResults();
                        tbResult = tbTemp.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.QG:
                    {
                        QRequisition[] xQRequsition = BaseDataProxy.FindRequisitions(edtCode.Text, edtOperator.Text, edtStatus.Text, edtBeginDate.Value, edtEndDate.Value, null, null, null);
                        // 按照 日期，单号 排序
                        IEnumerable<QRequisition> query =
                            from QRequisition in xQRequsition
                            orderby QRequisition.ReqDate, QRequisition.Code
                            select QRequisition;
                        tbResult = query.ToDataTable(); // xQRequsition.ToDataTable();                        
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.CGSH:
                case GRWL.BZSC.Common.EnumCommon.BillType.SCSH:
                    {
                        QShipping[] xQShipping = BaseDataProxy.FindShippings(BillPrefix, Code, Operator, null, BeginDate, EndDate, null, null, null, null);
                        // 按照 日期，单号 排序
                        IEnumerable<QShipping> query =
                            from QShipping in xQShipping
                            orderby QShipping.ShippingDate, QShipping.Code
                            select QShipping;
                        tbResult = query.ToDataTable(); // xQShipping.ToDataTable();
                        
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.LL:
                    {
                        QRecvMaterial[] QRecvMaterial = BaseDataProxy.FindRecvMaterials(Code, Operator, Status, BeginDate, EndDate, null, null, null);//(null, null, null, null, null, null, null, null);//
                        tbResult = QRecvMaterial.ToDataTable();
                        break;

                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.BJ:
                    {
                        QQuotation[] xQQuotation = BaseDataProxy.FindQuotations(Code, Operator, Status, BeginDate, EndDate, null, null, null);
                        // 按照 日期，单号 排序
                        IEnumerable<QQuotation> query =  
                            from QQuotation in xQQuotation
                            orderby QQuotation.QuoteDate, QQuotation.Code
                            select QQuotation;
                        tbResult = query.ToDataTable(); // xQQuotation.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.XQDD:
                    {
                        QOrder[] xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.DH.ToString(), BillPrefix, Operator, null, BeginDate, EndDate, null, null, null, false,true,null);
                        tbResult = xQOrder.ToDataTable();
                        this.SelectedItems = xQOrder;

                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCXQ:
                    {
                        QRequirement[] qRs = this.BaseDataProxy.FindRequirements(BillPrefix, Operator, Status, null, BeginDate, EndDate, null);
                        tbResult = qRs.ToDataTable();
                        this.SelectedItems = qRs;
                        break;
                    }
            }
            return tbResult;
        }

        private void CreateViewFromResult(DataTable TheRecords)
        {
            List<TableStruction> lTableColumns = new List<TableStruction>();

            //
            switch (SelectBillType)
            {
                case GRWL.BZSC.Common.EnumCommon.BillType.CG:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("OrderDate", "采购日期"));
                        lTableColumns.Add(new TableStruction("OrderCode", "单号"));
                        lTableColumns.Add(new TableStruction("TraderName", "供应商"));
                        lTableColumns.Add(new TableStruction("DeliveryDate", "交货日期"));
                        lTableColumns.Add(new TableStruction("MaterialName", "采购物资"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Num", "数量"));
                        lTableColumns.Add(new TableStruction("FinishedNum", "完成数量"));
                        lTableColumns.Add(new TableStruction("Status", "审批"));
                        break;
                    }

                case GRWL.BZSC.Common.EnumCommon.BillType.DH:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("OrderDate", "订货日期"));
                        lTableColumns.Add(new TableStruction("OrderCode", "单号"));
                        lTableColumns.Add(new TableStruction("TraderName", "客户"));
                        lTableColumns.Add(new TableStruction("DeliveryDate", "交货日期"));
                        lTableColumns.Add(new TableStruction("MaterialId", "订货物资编号", false));
                        lTableColumns.Add(new TableStruction("MaterialName", "订货物资"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Num", "数量"));
                        lTableColumns.Add(new TableStruction("FinishNum", "完成数量"));
                        lTableColumns.Add(new TableStruction("Status", "审批"));
                        break;
                    }

                case GRWL.BZSC.Common.EnumCommon.BillType.SCD:
                    {
                        lTableColumns.Add(new TableStruction("OrderCode", "订单号"));
                        lTableColumns.Add(new TableStruction("OrderDate", "订单日期"));
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("prdnDate", "日期"));
                        lTableColumns.Add(new TableStruction("Code", "生产单号"));
                        lTableColumns.Add(new TableStruction("Status", "批准"));
                        lTableColumns.Add(new TableStruction("materialcode", "产品代码"));
                        lTableColumns.Add(new TableStruction("MaterialName", "产品名称"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Category", "类别"));
                        lTableColumns.Add(new TableStruction("PrdnType", "生产类型"));
                        lTableColumns.Add(new TableStruction("DepartmentName", "生产部门"));
                        lTableColumns.Add(new TableStruction("PlanOutput", "计划产量"));
                        lTableColumns.Add(new TableStruction("FinishNum", "完成数量"));
                        break;
                    }

                case GRWL.BZSC.Common.EnumCommon.BillType.CGZJ:
                case GRWL.BZSC.Common.EnumCommon.BillType.SCZJ:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("QCDate", "质检日期"));
                        lTableColumns.Add(new TableStruction("Code", "质检单号"));
                        lTableColumns.Add(new TableStruction("materialcode", "物资代码"));
                        lTableColumns.Add(new TableStruction("MaterialName", "物资名称"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("checkNum", "检查数量"));
                        lTableColumns.Add(new TableStruction("deliveryNum", "交货数量"));
                        lTableColumns.Add(new TableStruction("operator", "检验人"));
                        break;

                    }

                case GRWL.BZSC.Common.EnumCommon.BillType.CGSH:
                case GRWL.BZSC.Common.EnumCommon.BillType.SCSH:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("ShippingDate", "日期"));
                        lTableColumns.Add(new TableStruction("Code", "单号"));
                        lTableColumns.Add(new TableStruction("TraderName", "供应商"));
                        lTableColumns.Add(new TableStruction("MaterialName", "物资"));
                        lTableColumns.Add(new TableStruction("ShippingNum", "数量"));
                        lTableColumns.Add(new TableStruction("MaterialSpecs", "规格"));
                        lTableColumns.Add(new TableStruction("Unit", "单位"));
                        lTableColumns.Add(new TableStruction("CategoryName", "分类"));
                        lTableColumns.Add(new TableStruction("Status", "审批"));
                        lTableColumns.Add(new TableStruction("orderCode", "订货单"));
                        lTableColumns.Add(new TableStruction("qcCode", "质检单"));
                        break;
                    }

                case GRWL.BZSC.Common.EnumCommon.BillType.LL:
                    {
                        lTableColumns.Add(new TableStruction("id", "编号", false));
                        lTableColumns.Add(new TableStruction("RqrtCode", "领料单号"));
                        lTableColumns.Add(new TableStruction("recvDate", "领料日期"));
                        lTableColumns.Add(new TableStruction("materialcode", "物料代码"));
                        lTableColumns.Add(new TableStruction("Materialname", "物料名称"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Num", "数量"));
                        //lTableColumns
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.QG:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("ReqDate", "请购日期"));
                        lTableColumns.Add(new TableStruction("Code", "单号"));
                        lTableColumns.Add(new TableStruction("Materialname", "物料名称"));
                        lTableColumns.Add(new TableStruction("CategoryName", "分类"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Num", "数量"));
                        lTableColumns.Add(new TableStruction("Trader", "供应商"));
                        lTableColumns.Add(new TableStruction("Status", "审批"));
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.BJ:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("quoteDate", "报价日期"));
                        lTableColumns.Add(new TableStruction("Code", "单号"));
                        lTableColumns.Add(new TableStruction("Materialname", "物料名称"));
                        lTableColumns.Add(new TableStruction("TraderName", "供应商"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Unit", "单位"));
                        lTableColumns.Add(new TableStruction("Price", "单价"));
                        //lTableColumns.Add(new TableStruction("minNum", "最少数量"));
                        //lTableColumns.Add(new TableStruction("maxNum", "最多数量"));
                        lTableColumns.Add(new TableStruction("Status", "审批"));
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.XQDD:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("OrderType", "类型"));
                        lTableColumns.Add(new TableStruction("OrderCode", "单号"));
                        lTableColumns.Add(new TableStruction("TraderName", "客户"));
                        lTableColumns.Add(new TableStruction("OrderDate", "订货日期"));
                        lTableColumns.Add(new TableStruction("DeliveryDate", "预交日期"));
                        lTableColumns.Add(new TableStruction("MaterialCode", "产品代号"));
                        lTableColumns.Add(new TableStruction("MaterialName", "产品名称"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("Category", "材料分类"));
                        lTableColumns.Add(new TableStruction("Num", "订货数量"));
                        lTableColumns.Add(new TableStruction("FinishedNum", "发货数量"));
                        lTableColumns.Add(new TableStruction("DetailMemo", "备注"));
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCXQ:
                    {
                        lTableColumns.Add(new TableStruction("Id", "编号", false));
                        lTableColumns.Add(new TableStruction("Code", "计划编号"));
                        lTableColumns.Add(new TableStruction("OrderCode", "订单号"));
                        lTableColumns.Add(new TableStruction("RqrtDate", "计划日期"));
                        lTableColumns.Add(new TableStruction("OperatorName", "制表人"));
                        lTableColumns.Add(new TableStruction("MaterialCode", "订单产品代号"));
                        lTableColumns.Add(new TableStruction("MaterialName", "产品名称"));
                        lTableColumns.Add(new TableStruction("Specs", "规格"));
                        lTableColumns.Add(new TableStruction("DepartmentName", "部门名称"));
                        lTableColumns.Add(new TableStruction("IsRelease", "发放完成"));
                        lTableColumns.Add(new TableStruction("Status", "审批"));
                        lTableColumns.Add(new TableStruction("AssessorName", "审批人"));
                        break;
                    }
            }
            this.SuspendLayout();
            CreateTableColumns(gvBill, lTableColumns);
            gvBill.DataSource = TheRecords;
            gvBill.AutoResizeColumns();
            this.ResumeLayout();
        }

        private void CreateTableColumns(UDataGridView TheView, List<TableStruction> TheList)
        {
            TheView.Columns.Clear();
            //TheView.ReadOnly = false;

            //DataGridViewColumn cl = new DataGridViewCheckBoxColumn();
            //cl.HeaderText = "选择";
            //cl.Name = "Select";
            //cl.CellTemplate = new DataGridViewCheckBoxCell();
            //cl.ReadOnly = false;
            //gvBill.Columns.Insert(0, cl);

            foreach (TableStruction TheStru in TheList)
            {
                TheView.Columns.Add(TheStru.TableFieldName, TheStru.TableFieldCaption);
                TheView.Columns[TheStru.TableFieldName].DataPropertyName = TheStru.TableFieldName;
                TheView.Columns[TheStru.TableFieldName].Visible = TheStru.Visible;
                TheView.Columns[TheStru.TableFieldName].ReadOnly = true;
            }
            //TheView.Columns["Id"].Visible = false;//隐藏Id
        }

        private object GetCellValue(DataGridViewRow rSel, string fName)
        {
            for (int iCount = 0; iCount < rSel.Cells.Count; iCount++)
            {
                if (rSel.Cells[iCount].OwningColumn.Name.ToLower() == fName.ToLower())
                {
                    return rSel.Cells[iCount].Value;
                }
            }
            return string.Empty;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            GetResult();
        }

        private void GetResult()
        {
            if (gvBill.SelectedRows.Count == 0)
            {
                MessageBox.Show("选择为空，请选择纪录后再确定。", "", MessageBoxButtons.OK);
                return;
            }

            try
            {
                if (this.gvBill.SelectedRows != null)
                {
                    ArrayList ary = new ArrayList();
                    for (int i = 0; i < gvBill.SelectedRows.Count; i++)
                    {
                        SelectBillsInfo newItem = new SelectBillsInfo();

                        newItem.BillId = (int)GetCellValue(gvBill.SelectedRows[i], "id");

                        if ((SelectBillType == GRWL.BZSC.Common.EnumCommon.BillType.CG) || (SelectBillType == GRWL.BZSC.Common.EnumCommon.BillType.DH))
                            newItem.BillCode = (string)GetCellValue(gvBill.SelectedRows[i], "OrderCode");
                        else
                            newItem.BillCode = (string)GetCellValue(gvBill.SelectedRows[i], "code");

                        string str = GetCellValue(gvBill.SelectedRows[i], "detailid").ToString();
                        if (str.ToString() != "")
                            newItem.BillDetailId = Convert.ToInt32(str);

                        str = GetCellValue(gvBill.SelectedRows[i], "MaterialId").ToString();
                        if (str.ToString() != "")
                            newItem.MateriailId = Convert.ToInt32(str);

                        SelectBills.Add(newItem);
                        ary.Add(Convert.ToInt32(this.gvBill.SelectedRows[i].Cells[0].Value));
                    }

                    switch (this.SelectBillType)
                    {
                        case GRWL.BZSC.Common.EnumCommon.BillType.XQDD:
                            this.SelectedItems = this.SelectedItems.Cast<QOrder>().Where(p => ary.Contains(p.Id)).ToArray();
                            break;
                        case GRWL.BZSC.Common.EnumCommon.BillType.SCXQ:
                            this.SelectedItems = this.SelectedItems.Cast<QRequirement>().Where(p => p.Id == Convert.ToInt32(this.gvBill.SelectedRows[0].Cells[0].Value)).ToArray();
                            break;
                    }
                }
            }
            catch { }
            this.DialogResult = DialogResult.OK;
            Close();
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void traderSelector_SelectorClick(object sender, EventArgs e)
        {
            TraderFrm frm = new TraderFrm();
            if (bSupplier)
            {
                frm.SearchSupplierMode(); // .SetTraderAsSupplier();
            }
            else
            {
                frm.SearchClientMode(); // .SetTraderAsClient();
            }
            if (frm.ShowDialog() == DialogResult.OK)
            {
                traderSelector.SetSelected(frm.SelectorItem);
            }

        }

    }
}
